package com.jk.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jk.blog.common.Constant;
import com.jk.blog.config.redis.RedisService;
import com.jk.blog.entity.ArticleInfo;
import com.jk.blog.entity.CommentInfo;
import com.jk.blog.entity.UserInfo;
import com.jk.blog.entity.enumType.CommentType;
import com.jk.blog.mapper.basic.ArticleInfoMapper;
import com.jk.blog.mapper.basic.CommentInfoMapper;
import com.jk.blog.mapper.read.ArticleInfoReadMapper;
import com.jk.blog.mapper.write.ArticleInfoWriteMapper;
import com.jk.blog.model.ArticleInfoResponse;
import com.jk.blog.model.Page;
import com.jk.blog.model.ResponseInfo;
import com.jk.blog.service.ArticleService;
import com.jk.blog.util.NumberUtils;
import com.jk.blog.util.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    /**
     * 日志对象
     */
    private static final Logger log = Logger.getLogger(ArticleServiceImpl.class);

    @Autowired
    private ArticleInfoMapper articleInfoMapper;

    @Autowired
    private CommentInfoMapper commentInfoMapper;

    @Autowired
    private ArticleInfoReadMapper articleInfoReadMapper;

//    private ArticleInfoReadMapper articleInfoReadMapper2;

    @Autowired
    private ArticleInfoWriteMapper articleInfoWriteMapper;

    @Autowired
    private RedisService redisService;

//    @Resource(name = "readSqlSessionFactory")
//    public void setArticleInfoReadMapper2(SqlSessionFactory readSqlSessionFactory){
//        SqlSession sqlSession = readSqlSessionFactory.openSession();
//        this.articleInfoReadMapper2 = sqlSession.getMapper(ArticleInfoReadMapper.class);
//    }

    @Override
    @Transactional
    public ResponseInfo addArticle(String articleTitle, String articleInfo, Integer publisherId) {
        if(StringUtils.isEmpty(articleTitle) || publisherId == null || publisherId == 0){ // 文章标题为空或者发布人ID为空
            return ResponseInfo.EMPTY_PARAMETER;
        }
        ArticleInfo article = new ArticleInfo();
        article.setArticleTitle(articleTitle); // 文章标题
        article.setArticleInfo(articleInfo); // 文章内容
        article.setPublisherId(publisherId); //发布人ID

        try{
            // 新增文章信息
            articleInfoWriteMapper.insertArticle(article);
            return ResponseInfo.SUCCESS;
        } catch (Exception e){
            // 异常输出
            log.error(e.getMessage(), e);
            return ResponseInfo.ERROR;
        }
    }

    @Override
    public ResponseInfo queryArticleInfo(Integer id) {
        if(id == null || id == 0){ //文章ID为空
            return ResponseInfo.EMPTY_PARAMETER;
        }
        try{
            ArticleInfo article;
            // 文章信息取得
//            article = articleInfoMapper.selectArticleInfoById(id);
            article = articleInfoReadMapper.selectArticleInfoById(id);
            // 验证mybatis缓存时间
            Date first = new Date();
            // 文章信息取得
//            article = articleInfoReadMapper2.selectArticleInfoById(id);
            log.info("first quest costs:"+ (new Date().getTime()-first.getTime()) +" ms");
            Date second = new Date();
//            article = articleInfoReadMapper2.selectArticleInfoById(id);
            log.info("second quest costs:"+ (new Date().getTime()-second.getTime()) +" ms");
            // 文章评论信息取得
            List<CommentInfo> commentList = commentInfoMapper.queryCommentList(id);

            // TODO 登录信息取得
            String strUserInfo = redisService.get(Constant.LOGIN_SESSION_KEY_PREFIX + 4);
            UserInfo userInfo = JSON.parseObject(strUserInfo, UserInfo.class);

            // 返回结果封装
            ArticleInfoResponse resArticleInfo = new ArticleInfoResponse();
            resArticleInfo.setArticle(article);
            resArticleInfo.setCommentList(commentList);
            resArticleInfo.setUserInfo(userInfo);

            return new ResponseInfo(ResponseInfo.SUCCESS, resArticleInfo);
        } catch (Exception e){
            // 异常输出
            log.error(e.getMessage(), e);
            return ResponseInfo.ERROR;
        }
    }

    @Override
    @Transactional
    public ResponseInfo addComment(Integer articleId, String commentInfo, Integer commentId, Integer createId) {
        if(articleId == null || articleId == 0 || StringUtils.isEmpty(commentInfo)){ //文章ID为空或者评论内容为空
            return ResponseInfo.EMPTY_PARAMETER;
        }
        CommentInfo comment = new CommentInfo();
        comment.setArticleId(articleId); // 文章ID
        comment.setCommentInfo(commentInfo); // 评论内容
        comment.setCreateId(createId); // 评论人ID
        if(NumberUtils.isEmpty(commentId)){ // 主题评论
            comment.setCommentType(CommentType.ARTICLE.getCode()); // 评论类型:1-主题评论
        } else { //评论的评论
            comment.setCommentId(commentId); // 评论ID
            comment.setCommentType(CommentType.COMMENT.getCode()); // 评论类型:2-评论回复
        }
        try {
            // 新增评论
            commentInfoMapper.insertCommentInfo(comment);

            // 评论列表获取
            List<CommentInfo> commentList = commentInfoMapper.queryCommentList(articleId);

            return new ResponseInfo(ResponseInfo.SUCCESS, commentList);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseInfo.ERROR;
        }
    }

    @Override
    public ResponseInfo queryArticleList(String articleTitle, int pageNo, int pageSize) {
        ArticleInfo article = new ArticleInfo();
        article.setArticleTitle(articleTitle);
        try {
            // 分页设置
            PageHelper.startPage(pageNo, pageSize);
            // 文章列表取得
            List<ArticleInfo> articleList = articleInfoMapper.selectArticleList(article);

            return new ResponseInfo(ResponseInfo.SUCCESS, new PageInfo<>(articleList));
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseInfo.ERROR;
        }
    }

    @Override
    @Transactional
    public ResponseInfo updateArticleInfo(String articleTitle, String articleInfo) {
        ArticleInfo article = new ArticleInfo();
        article.setArticleTitle(articleTitle); // 文章标题
        article.setArticleInfo(articleInfo); // 文章内容
        try {
            // 更新文章
            articleInfoMapper.updateByPrimaryKey(article);
            return ResponseInfo.SUCCESS;
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseInfo.ERROR;
        }
    }

}
